package com.java.androidmarket.controller;

import com.java.androidmarket.dto.StockDTO;
import com.java.androidmarket.entity.KillStock;
import com.java.androidmarket.entity.Result;
import com.java.androidmarket.service.GoodsService;
import com.java.androidmarket.service.KillStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * description:
 * author:张腾
 * date:2021-06-05
 */
@RestController
@RequestMapping("killStock")
@CrossOrigin
public class KillStockController {

    @Autowired
    private KillStockService killStockService;
    @Autowired
    private GoodsService goodsService;


    @PostMapping("get")
    public Result getStock(@RequestBody StockDTO stockDTO){

        HashMap<Integer, HashMap<String, Integer>> map = new HashMap<>();
        List<Integer> killgoodsIdList = stockDTO.getKillgoodsIdList();
        for (Integer killgoodsId : killgoodsIdList){
            KillStock stock = killStockService.getStock(killgoodsId);
//            stock.getStock();
//            stock.getSale();
            HashMap<String, Integer> stockMap = new HashMap<>();
            stockMap.put("stock",stock.getStock());
            stockMap.put("sale",stock.getSale());
            map.put(killgoodsId,stockMap);
        }

        return Result.success(map);
    }


    @GetMapping("getOne")
    public Result getOneStock(int killgoodsId){
        KillStock stock = killStockService.getStock(killgoodsId);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("stock",stock.getStock());
        map.put("sale",stock.getSale());
        return Result.success(map);
    }


}
